from django.shortcuts import render
from rest_framework.views import APIView
from .models import *
from .serializers import GroupSer,CateSer
from django.core.paginator import Paginator
from rest_framework.response import Response
# Create your views here.


# 获取全部分类分组
class CatesView(APIView):
    def get(self,request):
        # 接受前端数据 通过query_params传值
        page = request.query_params.get('page')
        pagesize = request.query_params.get('pagesize')

        print('dasfasf',page,pagesize)

        group_all = Group.objects.all()
        count = group_all.count()

        dada = Paginator(group_all,pagesize)

        deng = dada.page(page)

        ser = GroupSer(deng,many=True)

        return Response({
            'code':200,
            'msg':'返回成功',
            'results':ser.data,
            'count':count,
        })


class CatesViews(APIView):
    def get(self,request):
        # 接受前端数据 通过query_params传值

        group_all = Group.objects.all()

        ser = set([i.name for i in group_all])

        dada = [{'name':i} for i in ser]

        return Response(
            {
                'data':dada,
                'code':200,
            }
        )

# 添加
class AddView(APIView):
    def post(self, request):
        # 接受前端数据
        group_name = request.data.get('group_name')

        cate_name = request.data.get('category_name')

        url = request.data.get('url')

        sequence = request.data.get('sequence')

        print('组名',group_name)

        if not all([group_name,cate_name,url,sequence]):
            return Response({
                'code':500,
                'msg':'接受的前端数据为空'
            })

        #一级类别是否存在,

        try:
            cate = Cate.objects.get(name=cate_name)
        except Exception as e:
            # 此时说明该类别不存在
            cate = Cate.objects.create(name=cate_name)

        # 判断组名是否存在
        try:
            group = Group.objects.filter(name=group_name)[0]
        except Exception as e:
            return Response({
                'code':400,
                'msg':'没有次数据'
            })

        xiaodada = group.name + '/' + cate_name


        data = {
            "name": xiaodada,
            "cate": cate.id,
            "group_url": url,
            "sequence": sequence
        }
        # 使用序列化器添加数据
        ser = GroupSer(data=data)
        if ser.is_valid():
            ser.save()
        else:
            return Response({"msg": ser.error_messages, "code": 400})

        # # 判断组名是否存在
        try:
            cate_group = Group.objects.filter(name=group_name)[0]
        except Exception as e:
            print("改频道组不存在")
            return Response({'message':"频道组不存在","code":400})
        # 3. 修改频道组名字 电脑/ ----> 电脑/办公
        cate_group.name = xiaodada
        cate_group.save()
        # 3.返回
        return Response({'msg': "添加成功", "code": 201})



class GetGroupView(APIView):
    def get(self,request,id):
        try:
            group = Group.objects.get(id=id)
        except Exception as e:
            print('没有此信息',e)
            return Response({
                'code':500,
                'msg':'没有此信息'
            })

        ser = GroupSer(group)

        return Response(ser.data)



    # 修改
    def put(self,request,id):
        # 接受前端数据
        name = request.data.get('name')
        cate = request.data.get('cate')
        group_url = request.data.get('group_url')
        sequence = request.data.get('sequence')

        if not all([name,cate,group_url,sequence]):
            return Response({
                'code':400,
                'msg':'没有接受到数据',
            })

        try:
            cates = Cate.objects.get(name=cate)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'没有查询到信息'
            })

        cate_id = cates.id

        try:
            group = Group.objects.get(id=id)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'没有查询到信息'
            })

        try:
            group.name = name
            group.group_url = group_url
            group.cate_id = cate_id
            group.sequence = sequence
            group.save()
        except Exception as e:
            print('修改失败',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })

        return Response({
            'code':203,
            'msg':'修改成功'
        })

    def delete(self,request,id):
        try:
            Group.objects.get(id=id).delete()
        except Exception as e:
            print('删除错误',e)
            return Response({
                'code':400,
                'msg':'删除错误'
            })
        return Response({
            'code':204,
            'msg':'删除成功'
        })













# 获取所有的一级类别
class CatesAllView(APIView):
    def get(self,request):
        cate = Cate.objects.all()

        ser = []

        for i in cate:
            if not i.cid:
                adad = {
                    'id':i.id,
                    'name':i.name,
                    'cid':i.cid,
                }
                ser.append(adad)

        return Response(ser)


